/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* _reConstructBinaryTree(vector<int>& pre, vector<int>& vin, int& prei, int begin, int end)
    {
        if (begin > end)
            return nullptr;
        TreeNode* root = new TreeNode(pre[prei]);
        int i = begin;
        for (; i <= end; ++i)
        {
            if (vin[i] == pre[prei])
                break;
        }
        ++prei;
        root->left = _reConstructBinaryTree(pre, vin, prei, begin, i - 1);
        root->right = _reConstructBinaryTree(pre, vin, prei, i + 1, end);
        return root;
    }
    TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> vin) {
        int prei = 0;
        int end = vin.size() - 1;
        TreeNode* root = _reConstructBinaryTree(pre, vin, prei, 0, end);
        return root;
    }
};